//
//  LGTrainingProgramController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/31.
//  运动方案--训练项目

#import "LGTrainingProgramController.h"
#import "LGRecoverySectionView.h"
#import "LGRecoveryTrainingCell.h"
#import "LGPickerViewMax.h"
@interface LGTrainingProgramController ()<UITableViewDelegate,UITableViewDataSource>

/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
@end

@implementation LGTrainingProgramController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择训练项目";
    self.view.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.plan_action/actionList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName2 content:@"暂无训练项目" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName2 content:@"暂无训练项目" offset:0];
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(45);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSDictionary *tempDic = self.dataArray[section];
    LGRecoverySectionView *headView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(45))];
    headView.title = LGNSString(tempDic[@"category_name"]);
    return headView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *tempArray = self.dataArray[indexPath.section][@"items"];
    NSDictionary *tempDic = tempArray[indexPath.row];
    LGRecoveryTrainingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.showDesc = YES;
    cell.showTip = ((!_categoryid||_categoryid.length == 0)||[_categoryid isEqualToString:LGNSString(tempDic[@"category_id"])])?YES:NO;
    cell.type = @"separate";
    cell.dataDic = tempDic;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __block NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[indexPath.section][@"items"][indexPath.row]];
    if(!_categoryid||_categoryid.length == 0){
        //添加
        LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.unitOffset = viewPix(30);
        pickerView.pickArry = @[@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"],@[@"1",@"2",@"3",@"4",@"5",@"6",@"8",@"10",@"12",@"15",@"20",@"25",@"30"],@[@"无",@"5",@"10",@"15",@"20",@"25",@"30",@"40",@"50",@"60",@"80",@"100",@"120"]];
        pickerView.units = @[@"组",@"次",@"秒"];//
        pickerView.selectStr = @"3|3|5";
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewResult = ^(NSArray *resultArray) {
            [tempDic setValue:LGNSString(resultArray[0]) forKey:@"group_times"];
            [tempDic setValue:LGNSString(resultArray[1]) forKey:@"action_times"];
            [tempDic setValue:LGNSString(resultArray[2]) forKey:@"duration"];
            if(weakSelf.addTrainingData){
                weakSelf.addTrainingData(tempDic);
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    }else if([_categoryid isEqualToString:LGNSString(tempDic[@"category_id"])]){
        //编辑
        LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.unitOffset = viewPix(30);
        pickerView.pickArry = @[@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"],@[@"1",@"2",@"3",@"4",@"5",@"6",@"8",@"10",@"12",@"15",@"20",@"25",@"30"],@[@"无",@"5",@"10",@"15",@"20",@"25",@"30",@"40",@"50",@"60",@"80",@"100",@"120"]];
        pickerView.units = @[@"组",@"次",@"秒"];//
        pickerView.selectStr = @"3|3|5";
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewResult = ^(NSArray *resultArray) {
            [tempDic setValue:self.dataid forKey:@"id"];
            [tempDic setValue:LGNSString(resultArray[0]) forKey:@"group_times"];
            [tempDic setValue:LGNSString(resultArray[1]) forKey:@"action_times"];
            [tempDic setValue:LGNSString(resultArray[2]) forKey:@"duration"];
            if(weakSelf.editTrainingData){
                weakSelf.editTrainingData(tempDic);
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    }else{
        [TooltipView showMessage:@"此项目不可选" offset:0];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(5);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(5))];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGRecoveryTrainingCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
